import maya.cmds as mc
import maya.mel as mel
import sys
class updateVis():

    def __init__(self):
        if mc.objExists('visibility')==True:
            self.Vis='visibility'
        if mc.objExists('Visibility')==True:
            self.Vis='Visibility'
            
        if mc.objExists('visibility_ctrl')==True:
            self.Vis='visibility_ctrl'
        if mc.objExists('Visibility_ctrl')==True:
            self.Vis='Visibility_ctrl'
            
        if mc.objExists('global_ctrl')==True:
            self.Glb='global_ctrl'
        if mc.objExists('Global_ctrl')==True:
            self.Glb='Global_ctrl'
        if mc.objExists('of3d_rig')==True:
            self.of3d='of3d_rig'
        if mc.objExists('Of3d_rig')==True:
            self.of3d='Of3d_rig'
        if mc.objExists('all_ctrl')==True:
            self.all='all_ctrl'
        if mc.objExists('All_ctrl')==True:
            self.all='All_ctrl'

    def aa(self):
        
        if mc.objExists('visibility')==True or mc.objExists('Visibility')==True or mc.objExists('Visibility_ctrl')==True or mc.objExists('visibility_ctrl')==True:
            
            if mc.attributeQuery('controls_vis',node=self.Vis,exists=True)==False:
                
                mc.addAttr(str(self.Vis),longName='controls_vis',at='bool',k=False,h=False,w=True)
                mc.addAttr(str(self.Vis)+'.controls_vis',e=True,dv=1)
                mc.setAttr(str(self.Vis)+'.controls_vis',cb=True)
                mc.setAttr(str(self.Vis)+'.controls_vis',1)
            else:
                pass
            
            if mc.objExists('of3d_rig')==True or mc.objExists('Of3d_rig')==True:
                
                try:
                    attrBase=['translateX','translateY','translateZ','rotateX','rotateY','rotateZ','scaleX','scaleY','scaleZ','visibility']
                    #zeroGrp=mc.listRelatives(self.Vis,p=True)[0]
                    zeroGrp=str(self.Vis)+'_zero'
                    for  i in attrBase:
                        mc.setAttr(str(zeroGrp)+'.'+i,lock=False)
                    mc.parent(zeroGrp,self.of3d)
                    
                    if mc.objExists('all_ctrl')==True or  mc.objExists('All_ctrl')==True:
                        mc.scaleConstraint(self.all,zeroGrp,mo=True)
                        mc.parentConstraint(self.all,zeroGrp,mo=True)
                except:
                    pass
                mc.select('*_ctrl')
                allctrls=mc.ls(sl=True)
                for ct in allctrls:
                    if ct == self.Vis:
                        pass
                    else:
                        ctrlshapes=mc.listRelatives(ct,s=True)
                        try:
                            for cs in ctrlshapes:
                                try:
                                    mc.connectAttr(str(self.Vis)+'.controls_vis',str(cs)+'.visibility')
                                except:
                                    pass
                        except:
                            pass     
                mc.select(cl=True)     
                
                ###
                poleVecLine=['L_leg_pole_vec_ctrl_l_leg_drv_line','R_leg_pole_vec_ctrl_r_leg_drv_line','L_arm_pole_vec_ctrl_l_arm_ik_drv_line','R_arm_pole_vec_ctrl_r_arm_ik_drv_line',
                    'L_index_ctrl_l_index_jnt_sdk_line','L_middle_ctrl_l_middle_jnt_sdk_line','L_thumb_ctrl_l_thumb_jnt_sdk_line','L_ring_ctrl_l_ring_jnt_sdk_line','L_pinky_ctrl_l_pinky_jnt_sdk_line',
                    'R_index_ctrl_r_index_jnt_sdk_line','R_middle_ctrl_r_middle_jnt_sdk_line','R_thumb_ctrl_r_thumb_jnt_sdk_line','R_ring_ctrl_r_ring_jnt_sdk_line','R_pinky_ctrl_r_pinky_jnt_sdk_line']
                for pvl in poleVecLine:
                    try:
                        lineshapes=mc.listRelatives(pvl,s=True)
                        for lis in lineshapes:
                            try:
                                mc.connectAttr(str(self.Vis)+'.controls_vis',str(lis)+'.visibility')
                            except:
                                pass
                    except:
                        pass
                textGrp=['L_arm_state_text_grp','R_arm_state_text_grp','L_leg_state_text_grp','R_leg_state_text_grp']
                for tg in textGrp:
                    try :
                        mc.setAttr(str(tg)+'.visibility',lock=False,k=True)
                        if mc.connectionInfo(str(tg)+'.visibility',isExactSource=True)==False:
                            mc.connectAttr(str(self.Vis)+'.controls_vis',str(tg)+'.visibility')
                            mc.setAttr(str(tg)+'.visibility',lock=True,cb=False,k=False)
                    except:
                        pass
                ###       update for old version 2013/09/16 before
                
                if mc.connectionInfo(str(self.Glb)+'.visibility',isExactSource=True)==False:
                    
                    mc.setAttr(str(self.Glb)+'.visibility',lock=False,k=True)
                    try:
                        mc.disconnectAttr(str(self.Vis)+'.controls_vis', str(self.Glb)+'.visibility')
                    except:
                        pass
                    mc.setAttr(str(self.Glb)+'.visibility',1)
                    mc.setAttr(str(self.Glb)+'.visibility',lock=True,cb=False,k=False)
                    
                    
                if mc.objExists('ctrl')==True:
                    mel.eval('layerEditorDeleteLayer ctrl')
                else:
                    pass 
                    
                ######  update  by 2013/09/18
                handsecCtrl=['L_hand_driver_ctrl_zero','R_hand_driver_ctrl_zero','L_prop_driver_ctrl_zero','R_prop_driver_ctrl_zero']
                for hsc in handsecCtrl:
                    if mc.objExists(hsc)==True:
                        try:
                            mc.setAttr(str(hsc)+'.visibility',lock=False)
                            mc.connectAttr(str(self.Vis)+'.controls_vis', str(hsc)+'.visibility')
                            mc.setAttr(str(hsc)+'.visibility',lock=True)
                        except:
                            pass
                            
                
                
        sys.stdout.write("ok,control_is has connected all ctrls' visibility")
                    #if mc.objExists('global_ctrl')==True or mc.objExists('Global_ctrl')==True:
                    #    mc.setAttr(str(self.Glb)+'.visibility',lock=False,k=True)
                    #    if mc.connectionInfo(str(self.Glb)+'.visibility',isExactSource=True)==False:
                    #
                    #        mc.connectAttr(str(self.Vis)+'.controls_vis',str(self.Glb)+'.visibility')
                    #        mc.setAttr(str(self.Glb)+'.visibility',lock=True,cb=False,k=False)
                    #
                    #    else:
                    #        print('the atttibute of3d_rig.visibility has already connected')
                    #    
                    #    if mc.objExists('ctrl')==True:
                    #        mel.eval('layerEditorDeleteLayer ctrl')
                    #    else:
                    #        pass 
                    #   
                    #for h in attrBase:
                    #    mc.setAttr(str(zeroGrp)+'.'+h,lock=True)
        
            
    